#include "musictable.h"
#include "util.h"

MusicTable::MusicTable(QWidget *parent) : QTableView(parent)
{
    model = new QStandardItemModel(0, 4, this);
    model->setHorizontalHeaderItem(0, new QStandardItem("Song"));
    model->setHorizontalHeaderItem(1, new QStandardItem("Artist"));
    model->setHorizontalHeaderItem(2, new QStandardItem("Duration"));
    model->setHorizontalHeaderItem(3, new QStandardItem("Youtube Address"));
    setModel(model);

    setFocusPolicy(Qt::NoFocus);

    QHeaderView* view = new QHeaderView(Qt::Horizontal, this);
    view->setFixedHeight(20);
    view->resizeSection(0, 300);
    view->setHighlightSections(false);
    view->setClickable(true);
    view->setContextMenuPolicy(Qt::CustomContextMenu);
    setHorizontalHeader(view);

    setShowGrid(false);
    setEditTriggers(QTableView::NoEditTriggers);
    setSelectionBehavior(QTableView::SelectRows);
    setAlternatingRowColors(true);
    setSortingEnabled(true);

    verticalHeader()->hide();

    setMinimumWidth(350);

    hideColumn(3);

    setContextMenuPolicy(Qt::ActionsContextMenu);
    editAct = new QAction("Edit", this);
    addSongAct = new QAction("Add", this);
    deleteAct = new QAction("Delete", this);
    deleteAct->setShortcut(QKeySequence::Delete);
    addMenu = new QMenu(this);
    addSongAct->setMenu(addMenu);

    addAction(editAct);
    addAction(addSongAct);
    addAction(deleteAct);

    QObject::connect(deleteAct, SIGNAL(triggered()), this, SLOT(onDeleteClicked()));
    QObject::connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(songClicked(QModelIndex)));
    QObject::connect(editAct, SIGNAL(triggered()), this, SIGNAL(editClicked()));
}

MusicTable::~MusicTable()
{

}

void MusicTable::addToPlaylistMenu(QAction *act)
{
    addMenu->addAction(act);
}

void MusicTable::onDeleteClicked()
{
    VideoInfo video = getHighlightedVideo();
    model->removeRow(currentIndex().row());
    emit(deleteSongFromPlaylist(video));
}

VideoInfo MusicTable::getHighlightedVideo()
{
    return vids[currentIndex().row()];
}

void MusicTable::songClicked(QModelIndex Data)
{
    emit(songClicked(vids[Data.row()]));
}

void MusicTable::loadPlaylist(Playlist list)
{
    vids.clear();
    model->removeRows(0,model->rowCount());


    QList<VideoInfo> l = list.getVids();
    QList<VideoInfo>::Iterator itr = l.begin();
    while(itr != l.end())
    {
        vids.push_back(*itr);

        QList<QStandardItem*> items;
        items.push_back(new QStandardItem((*itr).title));
        items.push_back(new QStandardItem((*itr).artist));

        QTime duration(0, (*itr).duration/60, (*itr).duration % 60);
        items.push_back(new QStandardItem(duration.toString("m:ss")));
        items.push_back(new QStandardItem((*itr).id));
        model->appendRow(items);

        itr++;
    }
    qDebug()<<"musictable filling";
    resizeColumnsToContents();
}

void MusicTable::filter(QString word)
{
    qDebug("Commencing filter procedure");
    if(word.isEmpty())
    {
        for(int i = 0; i < model->rowCount(); i++)
            setRowHidden(i, false);
    }
    else
    {
        //Set ALL rows to hidden
        for(int i = 0; i < model->rowCount(); i++)
            setRowHidden(i, true);
        //Find the matches
        QList<QStandardItem*> matches;
        for(int i = 0; i < model->columnCount(); i++)
            matches += model->findItems(word, Qt::MatchContains, i);
        //Make the matching rows visible
        for(QList<QStandardItem*>::iterator itr = matches.begin(); itr != matches.end(); itr++)
            setRowHidden((*itr)->row(), false);
    }

}
